<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\ReportModel;

/**
 * SearchReportModel represents the model behind the search form about `common\models\ReportModel`.
 */
class SearchReportModel extends ReportModel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'referee'], 'integer'],
            [['id', 'referee', 'uid', 'sign', 'return_time', 'sign_time', 'sign_house'], 'integer'],
            [['contacts', 'phone', 'status'], 'safe'],
            [['return_amount', 'sign_amount'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $referee)
    {
        $query = ReportModel::find()->joinWith('user')->joinWith('house')->alias('r')->orderBy(['id' => SORT_DESC]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->sort = false;
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        if (!empty($referee)) {
            // grid filtering conditions
            $query->andFilterWhere([
                'r.referee' => $referee,
            ]);
        }

//        $query->andFilterWhere(['like', 'contacts', $this->contacts])
//            ->andFilterWhere(['like', 'phone', $this->phone])
//            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
